﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UcAsp.Net.Model;
namespace UcAsp.Net.NOSQLDAL
{
   public class DefinedCodeDAL : UcAsp.Net.IDAL.IDefinedCodeDAL
    {
        public bool Delete(UcAsp.Net.Model.DefinedCode code)
        {
            STSData.GetInstance.Delete<Model.DefinedCode>(code.ID);
            return true;
        }
        public bool Insert(UcAsp.Net.Model.DefinedCode code)
        {
            long key;
            Model.DefinedCode lastrow = STSData.GetInstance.LastRow<Model.DefinedCode>(out key);
            code.ID = Convert.ToInt32(key.ToString()) + 1;
            STSData.GetInstance.Insert<Model.DefinedCode>(code.ID, code);
            return true;
        }
        public System.Collections.Generic.List<UcAsp.Net.Model.DefinedCode> Select()
        {
            return STSData.GetInstance.Select<UcAsp.Net.Model.DefinedCode>();
        }
        public System.Collections.Generic.List<UcAsp.Net.Model.DefinedCode> SelectGroupType(string typename)
        {
            return Select().Where(o => o.DefinedCodeType == typename).ToList();
        }
        public bool Update(UcAsp.Net.Model.DefinedCode code)
        {
            STSData.GetInstance.Update<Model.DefinedCode>(code.ID, code);
            return true;
        }
        public UcAsp.Net.Model.DefinedCode GetDefinedCodeName(int id)
        {
            DefinedCode code = Select().FirstOrDefault(o => o.ID == id);
            return code;
        }
        public List<UcAsp.Net.Model.DefinedCode> Select(int pagesize, int pageindex, out int pagecount)
        {
            List<DefinedCode> list = new List<DefinedCode>();
            var query = Select();
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }
    }
}
